package com.yohann.course_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.course_service.entity.Course;
import com.yohann.course_service.entity.Subject;
import com.yohann.course_service.entity.vo.AnswerVO;
import com.yohann.course_service.entity.vo.CheckResult;
import com.yohann.course_service.entity.vo.CourseInfo;
import com.yohann.course_service.mapper.CourseMapper;
import com.yohann.course_service.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    /**
     * 根据id查询课程
     *
     * @param id
     * @return
     */
    @Override
    public CourseInfo getCourseInfoById(int id) {
        return baseMapper.getCourseInfoById(id);
    }

    /**
     * 分页查询课程分类
     */
    @Override
    public Map<String, Object> pageQueryCourse(int current, int limit, String title) {
        //将起始位置、结束位置存入map
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("current", (current - 1) * limit);
        queryMap.put("limit", limit);

        //判断是否存在查询条件
        if (StringUtils.hasLength(title)) {
            queryMap.put("title", title);
        }

        //通过分页 查询课程信息
        List<CourseInfo> courseInfoList = baseMapper.pageQueryCourse(queryMap);

        //查询该条件下数据的数量
        Integer count = baseMapper.pageQueryCourseCount(queryMap);

        //将结果存入map中并返回
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("rows", courseInfoList);
        return map;
    }

    /**
     * 查询课程
     *
     * @param title 课程名称
     * @return 课程id集合
     */
    @Override
    public List<Integer> searchCourse(String title) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Course::getTitle, title);

        return baseMapper.selectList(wrapper).stream().map(Course::getId).collect(Collectors.toList());
    }

    /**
     * 课程TOP3
     *
     * @return 课程集合
     */
    @Override
    public List<Course> getTop3() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create").last("limit 3");

        return list(wrapper);
    }

    @Override
    public List<CourseInfo> getCourseInfoByIds(List<Integer> ids) {
        return baseMapper.getCourseInfoByIds(ids);
    }

    @Override
    public List<CheckResult> checkAnswer(AnswerVO answer) {
        List<CheckResult> results = new ArrayList<>();

        for (int i = 0; i < answer.getMatch().length(); i++) {
            CheckResult checkResult = new CheckResult();
            results.add(checkResult);
            char[] an = answer.getAnswer().toCharArray();
            char[] match = answer.getMatch().toCharArray();

            checkResult.setAnswer(String.valueOf(an[i]));
            checkResult.setMatch(String.valueOf(match[i]));
            if (String.valueOf(an[i]).equalsIgnoreCase(String.valueOf(match[i]))) {
                checkResult.setResult("√");
            } else {
                checkResult.setResult("×");
            }
        }

        return results;
    }
}
